package com.semidata.trp.decision.request;

import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;

import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.rtd.core.log.Logger;
import com.semidata.rtd.engine.job.Job;
import com.semidata.rtd.engine.job.SyncTask;
import com.semidata.trp.decision.job.DecisionJob;
import com.semidata.trp.decision.utils.Utils;

public class TaskRequest extends SyncTask{
	
	public TaskRequest(Job job, long timeout) {
		super(job, timeout);
	}
	@Override
	public void run() throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = ((DecisionJob)getJob()).getJobContext().getHttpSession();
        Request request = ((DecisionJob)getJob()).getJobContext().getRequest();
        HttpRequest httpRequest = ((DecisionJob)getJob()).getJobContext().getHttpSession().getRequest();
        QueryStringDecoder decoder = new QueryStringDecoder(httpRequest.getUri());
        request.setPositionId(Integer.parseInt(decoder.parameters().get("p").get(0)));

        List<String> callbackList = decoder.parameters().get("c");
        if (callbackList == null || callbackList.isEmpty())
            request.setCallback(null);
        else
            request.setCallback(callbackList.get(0));

        List<String> userList = decoder.parameters().get("u");
        String userId = null;
        if (userList != null && !userList.isEmpty()) {
            userId = userList.get(0);
            if (userId != null && userId.isEmpty())
                userId = null;
        }

        if (userId == null) {
            userId = Utils.getUserId(session);
//            System.out.println("new userid is : " + userId);
        }

        request.setUserId(userId);

        List<String> ipList = decoder.parameters().get("ip");
        String ip = null;
        if (ipList != null && !ipList.isEmpty()) {
            ip = ipList.get(0);
            if (!Utils.isValidIp(ip))
                ip = null;
        }

        if (ip == null)
            ip = Utils.getIp(session);
        request.setIp(ip);
        
        List<String> key1 = decoder.parameters().get("k1");
        List<String> key2 = decoder.parameters().get("k2");
        List<String> key3 = decoder.parameters().get("k3");
        String keyword1 = null;
		String keyword2 = null;
		String keyword3 = null;
		boolean hasKey1 = (key1 != null && !key1.isEmpty() && !key1.get(0).trim().isEmpty());
		boolean hasKey2 = (key2 != null && !key2.isEmpty() && !key2.get(0).trim().isEmpty());
		boolean hasKey3 = (key3 != null && !key3.isEmpty() && !key3.get(0).trim().isEmpty());
        if (hasKey1) {
        	keyword1 = new String(key1.get(0).trim().getBytes(),"UTF-8");
        	if (hasKey2) {
        		keyword2 = new String(key2.get(0).trim().getBytes(),"UTF-8");
        		if (hasKey3) {
        			keyword3 = new String(key3.get(0).trim().getBytes(),"UTF-8");
        			Logger.log("DEBUG", keyword1 + "-" + keyword2 + "-" + keyword3);
        		}
        	} else {
        		if (hasKey3) {
        		    Logger.log("DEBUG", "k1 k2 k3 parameters error");
        		    throw new Exception("request fail");
        		}
        	}
        } else {
        	if (hasKey2 || hasKey3) {
        		Logger.log("DEBUG", "error request url in request");
        		throw new Exception("request fail");
        		
        	}
        }
        request.setKeyword1(keyword1);
    	request.setKeyword2(keyword2);
    	request.setKeyword3(keyword3);
	}

}
